/*
 * Created on Oct 15, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.erclab.internal.xpresso.formelements;

import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.dataAccess.ApplicationDataSource;
import com.erclab.internal.xpresso.dataAccess.GenericResultSet;
import com.erclab.internal.xpresso.exceptions.DataSourceException;
import com.erclab.internal.xpresso.exceptions.FormException;
import com.erclab.internal.xpresso.forms.Form;

/**
 * @author rodrigen
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class DropDown extends FormElement{

	protected String[] display;
	protected String[] value;
	protected String firstOption = "Select an option ...";

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.formelements.FormElement#toHTML(com.erclab.internal.xpresso.forms.Form, com.erclab.internal.xpresso.forms.FormParameters)
	 */
	public String toHTML(Form theForm) {
		StringBuffer sb = new StringBuffer();
		sb.append("<SELECT name=\"Field");
		sb.append(this.idFormElement);
		sb.append("\"");
		sb.append(" id=\"" + this.destinationColumn + "\" onChange=\"formElementChanged(this)\">");
		
		// TODO: Replace default text option:
		sb.append("<OPTION value=" + '"' + "-" + '"' + ">" + firstOption + " </OPTION>");
		
		for (int i =0; i < display.length; i++){
			sb.append("<OPTION VALUE=" + '"' + value[i] + '"');
			if(originalValue != null && originalValue.equalsIgnoreCase(value[i])){
				sb.append(" selected>" + display[i] + "</OTPION>");
			} else {
				sb.append(">" + display[i] + "</OTPION>\n");
			}
		}
		sb.append("</SELECT>");
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.formelements.FormElement#loadLookupValues(com.erclab.internal.xpresso.forms.FormParameters)
	 */
	public void loadLookupValues(Form theForm) throws FormException {
	
		ApplicationDataSource ads = Settings.getRepository().getApplicationDataSource(this.lookupDataSourceId);
		if (ads == null){
			throw new FormException("The data source specified by the element does not exist");	
		}
	
		String query = ads.getQueryBuilder().modifyLookupQuery(this.lookupQuery, theForm.getFormParameters());
		GenericResultSet grs = null;
		try {
			grs = ads.runQuery(query);
		} catch (DataSourceException e) {
			throw new FormException("An error ocurred loading form element lookup. Query:" + this.lookupQuery + "; Exception: " + e);
		}
	
		int lookupResults = grs.getNumberOfRows();
		int i = 0;
		if (lookupResults > 0){
			display = new String[lookupResults];
			value = new String[lookupResults];
			while(grs.nextRow()){
				value[i] = grs.getRow()[0];
				display[i] = grs.getRow()[1];
				i++;
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.formelements.FormElement#isModifiable()
	 */
	public boolean isModifiable() {
		return true;
	}



}
